package malus.shaofeng.com.beiwomusic.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.model.NewStory;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;
import malus.shaofeng.com.beiwomusic.utils.Constants;

/**
 * Created by aaa on 15-3-10.
 */
public class NewStoryListAdapter extends BaseAdapter{

    private Context context;
    private List<NewStory> newStories;

    public NewStoryListAdapter(Context context, List<NewStory> newStories) {
        this.context = context;
        this.newStories = newStories;
    }

    @Override
    public int getCount() {
        return newStories.size();
    }

    @Override
    public Object getItem(int position) {
        return newStories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (convertView!=null){
            ret=convertView;
        }else {
            ret= LayoutInflater.from(context).inflate(R.layout.item_new_story,parent,false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder==null){
            holder=new ViewHolder();
            holder.image=(ImageView)ret.findViewById(R.id.item_new_story_image);
            holder.title=(TextView)ret.findViewById(R.id.item_new_story_title);
            holder.userName=(TextView)ret.findViewById(R.id.item_new_story_userName);
            holder.forwards=(TextView)ret.findViewById(R.id.item_new_story_forwards);
            ret.setTag(holder);
        }

        NewStory newStory = newStories.get(position);
        holder.title.setText(newStory.getTitle());
        holder.userName.setText("作者："+newStory.getUser().getNickname());
        holder.forwards.setText("转发："+newStory.getForwards());

        holder.image.setImageResource(R.drawable.list_empty_bg);

        String image_uri = newStory.getImage_uri();
        if (image_uri!=null&&image_uri.length()>0){
            String[] split = image_uri.split("/");
            String path="";
            for (int j = 3; j < split.length; j++) {
                path = path+split[j];
                if(j!=split.length-1){
                    path+="/";
                }
            }
            path = Constants.URL.IMAGE_URL+path;
            BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
            bitmapUtils.display(holder.image,path,new BitmapLoadCallBack<ImageView>() {
                @Override
                public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                    imageView.setImageBitmap(bitmap);
                }

                @Override
                public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                    Log.e("asd", "asd" + s);
                }
            });

        }

        return ret;
    }

    private static class ViewHolder{
        public ImageView image;
        public TextView title;
        public TextView userName;
        public TextView forwards;
    }

}
